authApp.value('page','material_apply'); 
authApp.controller("applyMController",function($scope,$http,$location,$getQueryString,$splice){
	$scope.preNum=$getQueryString("preNum");
	$scope.isAddM=true;
    $scope.showEdit = false;
    
    $scope.schools = $getQueryString("school_type");
	var requestJson={
        'fid':$getQueryString("pid"),
        'school_type': $scope.schools
    };
	$scope.addM=function(){
		$scope.isAddM=false;
	}
	$scope.cancelAddM=function(){
		$scope.isAddM=true;
		$scope.addFZ="";
		//$scope.addFZNum="";
        $scope.addRequire = "";
        $scope.addTitle = ""
    }
    
    $scope.changes = function() {
      $scope.schools = $scope.schools
    }

	$scope.addFirstZ=function(){
        if($scope.addRequire && $scope.addTitle){
            if($scope.addFZ){
                var rJson={
                    'content':$scope.addFZ,
                    'fid':$getQueryString("pid"),
                    'name':$scope.addRequire,
                    'title': $scope.addTitle,
                    "school_type": parseInt($scope.schools),
                    "number": $scope.list.length + 1
                };

                $http({
                    method:'post',
                    url:apis.saveDetailedInfo,
                    data:rJson,
                }).success(function(response){
                    var radom=Math.random();
                    if(response.code=="000000"){
                        $scope.isAddM=true;
                        $scope.getAML();
                        $scope.addFZ="";
                        $scope.addRequire = "";
                    }else{
                        alert(response.errorMsg);
                    }
                }).error(function(){
                    alert("服务器打瞌睡了，请稍等！");
                    $scope.isAddM=true;
                })
            }else{
                alert("请输入评估标准");
            }
        }else{
            alert("请输入建设要求");
        }
	}
	//编辑
	$scope.edit = function (item) {
        $scope.item = item;
        $scope.showEdit = true;
    }
    $scope.doEdit = function (item) {
        /*if(!item.number){
            alert("请填写序号");
            return;
        }
        if(!item.content) {
            alert("请输入二级指标标题");
            return;
        }*/
        if(!item.s_title) {
            alert("请输入内容");
            return;
        }
        if(!item.s_name){
            alert("请输入建设要求");
            return;
        }
        if(!item.s_content){
            alert("请输入评估标准");
            return;
        }

        var rJson={'content':item.s_content,'name':item.s_name,'pid':item.p_id, 'title': item.s_title};
        $http({
            method:'post',
            url:apis.updataDetailedInfo,
            data:rJson,
        }).success(function(response){
            if(response.code=="000000"){
                alert("编辑成功");
                $scope.showEdit=false;
                $scope.getAML();
                $scope.item="";
            }else{
                alert(response.errorMsg);
            }
        }).error(function(){
            alert("服务器打瞌睡了，请稍等！");
            $scope.showEdit=false;
        })
    }
    $scope.doCancel = function () {
        $scope.showEdit=false;
    }
	$scope.getAML=function(){
		$http({
			method:'post',
			url:apis.queryDetailed,
			data:requestJson,
		}).success(function(response){
			if(response.code=="000000"){
				$scope.list=response.data;
                angular.forEach($scope.list,function(data,index,array){
                    data.number = index + 1;
                });
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等！");
		})
	}
	$scope.delCollect=function(pid){
		if(window.confirm("是否确定删除？")){
			var rJson={'pid':pid};
			$http({
				method:'post',
				url:apis.deleteDetailedInfo,
				data:rJson,
			}).success(function(response){
				if(response.code=="000000"){
					$scope.getAML();
				}else{
					alert(response.errorMsg);
				}
			})
		}
	}
    $scope.turnTo=function(pid){
        location.href="observationPoint.html?pid="+pid;
    }
	$scope.getAML();
})




authApp.directive('observe', function(){
    return {
        restrice: 'EA',
        controller: function($scope, pagerConfig){
            // 共多少条
            $scope.totalItems = 0;
            $scope.page = [];
            // 偏移数
            $scope.offsetPage = 0;
            // 一页多少条
            $scope.itemsPerpage = 0;
            // 一个多少页
            $scope.totalPages = 0;
            $scope.currentPage = 0;

            $scope.$watch('totalItems', function(){
                $scope.totalPages = Math.ceil($scope.totalItems / $scope.itemsPerpage);

                resetPageList();
                if ($scope.page[$scope.currentPage]) {
                    $scope.page[$scope.currentPage].active = true;
                }
            });

            var resetPageList = function(){
                $scope.page = [];

                var last = Math.min(Number($scope.offsetPage) + Number($scope.listSizes), $scope.totalPages);

                for (var i = $scope.offsetPage; i < last; i ++) {
                    $scope.page.push({
                        text: i,
                        indexPage: i,
                        active: false
                    })
                }

            }
            var getOffset = function(index){
                var offset = Math.min(index, $scope.totalPages - $scope.listSizes);
                if (offset <= 0) {
                    offset = 0;
                }
                return offset;
            };
            $scope.selectPage = function(index){
                if (index < 0 || index >= $scope.totalPages) {
                    return;
                }
                if ($scope.page[$scope.currentPage-$scope.offsetPage]) {
                    $scope.page[$scope.currentPage-$scope.offsetPage].active = false;
                }
                $scope.currentPage = index;
                // 如果currentPage 小于 offsetPage 或者 currentPage 大于 offsetPage加listsizes

                if ($scope.currentPage < $scope.offsetPage || $scope.currentPage >= $scope.offsetPage + $scope.page.length) {

                    $scope.offsetPage = getOffset(index)

                    resetPageList();
                }

                if ($scope.page[$scope.currentPage-$scope.offsetPage]) {
                    $scope.page[$scope.currentPage-$scope.offsetPage].active = true;
                }
                $scope.$emit('pagechage', $scope.currentPage);
            };
            $scope.next = function(){
                if ($scope.isLast()) {
                    return;
                }
                $scope.selectPage($scope.currentPage + 1);
            };
            $scope.provie = function(){
                if ($scope.isFirst()) return
                $scope.selectPage($scope.currentPage - 1);
            }
            $scope.first = function(){
                $scope.selectPage(0);
            }
            $scope.last = function(){
                $scope.selectPage($scope.totalPages - 1);
            }
            $scope.isFirst = function(){
                return $scope.currentPage <= 0;
            };
            $scope.isLast = function(){
                return $scope.currentPage >= $scope.totalPages - 1;
            }
            $scope.getText = function(key) {
                return pagerConfig.text[key];
            };


        },
        link: function(scope, ele, attrs){

            scope.itemsPerpage = attrs.itemsperpage || 1;
            scope.listSizes = attrs.listsizes;
            attrs.$observe('totalitems', function(val){
                scope.totalItems = val;
            })
        },
        templateUrl: '../../views/modal/page.html'
    }
}).constant('pagerConfig', {
        text: {
            'first': '首页',
            'provie': '上一页',
            'next': '下一页',
            'last': '尾页',
        }
    }
);
	